import bpy
import sys, os

filePath = os.environ["BlenderImageTranscoder-filePath"]

typeName = os.environ["BlenderImageTranscoder-typeName"]
objectName = os.environ["BlenderImageTranscoder-object"]
tempfilePath = os.environ["BlenderImageTranscoder-tempfilePath"]

sce = bpy.data.scenes[0]

#Remove all objects
for ob in sce.objects:
  print("ALL: Removed object ", ob.name)
  sce.objects.unlink(ob)

#Link the given object to the new file
path = os.path.join(filePath, 'Object/')
print('directory:', path)
bpy.ops.wm.link_append(directory=path, filename=objectName, link=False)


sce.render.resolution_x = 4
sce.render.resolution_y = 4
sce.render.resolution_percentage = 50

fields = ['use_antialiasing', 'use_sss', 'use_textures', 'use_shadows', 'use_envmaps', 'use_raytrace', 'use_color_management']

#Disable for quick render.
for field in fields:
  setattr(sce.render, field, False)
  
bpy.ops.render.render('EXEC_DEFAULT')

#Reset
for field in fields:
  setattr(sce.render, field, True)
sce.render.antialiasing_samples = '16'

if not os.path.exists(os.path.dirname(tempfilePath)):
  os.makedirs(os.path.dirname(tempfilePath))
  
bpy.ops.wm.save_as_mainfile(filepath=tempfilePath)


